from pms.common.views import PostFormula
from project.utils.list import list_projects
from user.utils.login import get_user_by_uid, UserNotExist


class API(PostFormula):
    def handle(self, req):
        try:
            uid = int(req['uid'])
            user = get_user_by_uid(uid)
            projects = list_projects(user).values('id', 'name')
            # print(projects)
            return self.success_respond({
                'list': list(projects)
            })
        except UserNotExist:
            return self.failed_respond('NO_SUCH_USER', '不存在此用户')
